package com.daocao.yfb;

import android.*;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.daocao.yfb.base.BaseActivity;
import com.daocao.yfb.fragment.MainDiscoverFragment;
import com.daocao.yfb.fragment.MainHomeFragment;
import com.daocao.yfb.fragment.MainMyFragment;
import com.daocao.yfb.view.HomeViewPager;
import com.daocao.yfb.view.tablayout.MainBottomTabLayout;
import com.daocao.yfb.view.uploadImage.UploadImgAdapter;

public class MainActivity extends BaseActivity {

    @BindView(R.id.viewpager)
    HomeViewPager pager;
    @BindView(R.id.tab_bottom)
    MainBottomTabLayout tabBottom;


    private Unbinder unbiner;
    private UploadImgAdapter uploadImgAdapter;
    private MainMyFragment fragmentMy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBar();
        unbiner = ButterKnife.bind(this);
        fragmentMy = new MainMyFragment();
        Fragment[] fragments = {new MainHomeFragment(), new MainDiscoverFragment(), fragmentMy};
//        String[] titles = new String[]{"首页", "发现", "我的"};
        pager.setOffscreenPageLimit(3);
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
        tabBottom.setViewPager(pager);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    fragmentMy.getUploadHead(file);
                }
            }
        });
    }

    public void goToTakePhoto() {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    private static long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(this,
                        "再按一次退出",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                this.finish();
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
